<?php
/**
 * Created by PhpStorm.
 * User: 17208
 * Date: 2020/9/28
 * Time: 16:08
 */
namespace app\controller;

use app\model\TbActivity;
use app\server\ServerTaoBao;
use app\server\taobao\ErrorCode;
use app\utility\TaoBaoApp;
use extend\Common;
use extend\Response;
use extend\ScRedis;

class Taobao
{
    public function getActivity(){
        try{
            $pid_data = TaoBaoApp::getPid();

            $params = [
                'adzone_id'=>$pid_data['adzone_id'],
            ];

            $cache_key = __FILE__.md5(json_encode($params));
            $data = json_decode(ScRedis::getInstance()->get($cache_key));
            if(!$data){
                $app = TaoBaoApp::get();

                $data = [];
                // 获取活动场ID
                $activitys = TbActivity::select(['id','activity_id','name','pic_url'],[]);
                foreach ($activitys as $activity){
                    $params['activity_material_id'] = $activity['activity_id'];
                    $item = $app->activity->infoGet($params);

                    $pwd_params = [
                        'text'=>$activity['name'],
                        'url'=>$item['data']['click_url'],
                    ];
                    $password = $app->tpwd->create($pwd_params);

                    $data[] = [
                        'name'=>$activity['name'],
                        'pic_url'=>$activity['pic_url'],
                        'password'=>$password['data']['model'],
                        'click_url'=>$item['data']['click_url'],
                    ];
                }
                // 设置缓存
                ScRedis::getInstance()->set($cache_key,json_encode($data),3600);
            }
            Response::res_ok($data);
        }catch (\Exception $e){
            Response::res_exp($e);
        }
    }
    public function tpwdParse(){
        $text = isset($_GET['password'])?$_GET['password']:'';
        try{
            $password = Common::tbkParsePassword($text);
            if(!$password){
                throw new \Exception(ErrorCode::$errorMsg[ErrorCode::$passwordError]);
            }
            list($errcode,$messgae) = ServerTaoBao::parseTkl2Url($password);
            if($errcode == 2) throw new \Exception($messgae,-1);
            if($errcode) throw new \Exception($messgae);
            Response::res_ok($messgae);
        }catch (\Exception $e){
            Response::res_exp($e);
        }
    }
    public function tpwdCreate(){
        $app = TaoBaoApp::get();
        $text = isset($_GET['text'])?$_GET['text']:1;
        $url = isset($_GET['url'])? $_GET['url']:'';
        $item_id = isset($_GET['item_id'])? $_GET['item_id']:'';

        if(!$url && $item_id){
            // 优惠卷查询
            $pid_data = TaoBaoApp::getPid();
            $params = [
                "itemid"=>$item_id,
                "site_id"=>$pid_data['site_id'],
                "adzone_id"=>$pid_data['adzone_id'],
            ];
            $coupon = $app->tkl->TbkPrivilegeGet($params);
            $url = $coupon['result']['data']['coupon_click_url'];
        }

        if(!$url) throw new \Exception("查无优惠卷");

        $reg = "#^http[s]?(.+)#isu";
        preg_match ($reg, $url, $m);
        !$m && $url = 'https:'.$url;

        try{
            if(!$text || !$url){
                throw new \Exception("参数text，url错误");
            }
            $params = [
                'text'=>$text,
                'url'=>$url
            ];
            $cache_key = __FILE__.md5(json_encode($params));
            $data = json_decode(ScRedis::getInstance()->get($cache_key));
            if(!$data){
                $data = $app->tpwd->create($params);
                $data = $data['data'];
                // 设置缓存
                ScRedis::getInstance()->set($cache_key,json_encode($data),3600);
            }
            Response::res_ok($data);
        }catch (\Exception $e){
            Response::res_exp($e);
        }
    }
    public function dGmaterialOptional(){
        $app = TaoBaoApp::get();
        $pid_data = TaoBaoApp::getPid();
        $page_no = isset($_GET['page'])?$_GET['page']:1;
        $q = isset($_GET['keyword'])?$_GET['keyword']:'';

        try{
            if(!$q){
                $_GET['material_id'] = 6708;
                $this->dGmaterialOptimus();
            }
            $params = [
                'page_size'=>20,
                'adzone_id'=>$pid_data['adzone_id'],
                'page_no'=>$page_no,
                'material_id'=>17004,
                'q'=>$q
            ];
            $cache_key = __FILE__.md5(json_encode($params));
            $data = json_decode(ScRedis::getInstance()->get($cache_key));
            if(!$data){
                $list = $app->dg->materialOptional($params);
                $data = [];
                foreach ($list['result_list']['map_data'] as $key=>&$datum){
                    if(!$datum['coupon_amount'])
                        continue;

                    $datum['coupon_price'] = bcsub($datum['zk_final_price'] ,$datum['coupon_amount'],2);
                    $data[] = $datum;
                }
                // 设置缓存
                ScRedis::getInstance()->set($cache_key,json_encode($data),360);
            }
            Response::res_ok($data);
        }catch (\Exception $e){
            Response::res_exp($e);
        }
    }
    public function dGmaterialOptimus(){
        // 0.聚划算相关 聚划算内的有满减或满折类的商品库获取。32366，31370
        // 1.淘抢购相关 淘抢购商品库 34616
        // 2.天天特卖 低价好货，工厂产业带爆款。 31362
        // 3.有好货精品 商品本身是受欢迎的品质好货，淘宝有好货的产品心智  4092
        // 4.实时热销榜 淘宝客渠道每日实时热销爆款，官方数据实时更新。
        // 综合	    大服饰	大快消	电器美家
        // 28026	28029	28027	28028
        // 5.大额券    大面额折扣超高佣金，每单赚更多
        // 综合	    女装	    食品	    美妆个护	家居家装	母婴
        // 27446	27448	27451	27453	27798	27454
        // 6.品牌券
        // 综合	    3786	 	 鞋包配饰	3796	 	 母婴	    3789
        // 女装  	3788	 	 美妆个护	3794	 	 食品	    3791
        // 家居家装	3792	 	 男装	    3790	 	 运动户外	3795
        // 数码家电	3793	 	 内衣	    3787
        $page_no = isset($_GET['page'])?$_GET['page']:1;
        $material_id = isset($_GET['material_id'])?$_GET['material_id']:4093;

        try{
            $app = TaoBaoApp::get();
            $pid_data = TaoBaoApp::getPid();
            $params = [
                'page_size'=>in_array($material_id,[31371,31370])?100:10,
                'adzone_id'=>$pid_data['adzone_id'],
                'page_no'=>$page_no,
                'material_id'=>$material_id,
                'has_coupon'=>true
            ];

            // 尝试从缓存中获取
            $cache_key = __FILE__.md5(json_encode($params));
            $data = json_decode(ScRedis::getInstance()->get($cache_key));
            if(!$data){
                $list = $app->dg->materialOptimus($params);
                $data = [];

                foreach ($list['result_list']['map_data'] as $datum){
                    if(!$datum['coupon_amount']) continue;
                    $datum['coupon_price'] = bcsub($datum['zk_final_price'] ,$datum['coupon_amount'],2);
                    !$datum['coupon_share_url'] && $datum['coupon_share_url'] = '';
                    $data[] = $datum;
                }
                // 设置缓存
                ScRedis::getInstance()->set($cache_key,json_encode($data),360);
            }
            Response::res_ok($data);
        }catch (\Exception $e){
            Response::res_exp($e);
        }
    }
}